package com.wisdom.smartbook.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Region;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceView;

import com.wisdom.smartbook.activity.FaceRecognizeActivity;
import com.wisdom.smartbook.utils.KLog;

/**
 * @ClassName: CircleCameraPreview
 * @Description: java类作用描述
 * @Author: xjliu
 * @CreateDate: 2023/6/19 5:15 下午
 * @Version: 1.0
 */

public class CircleCameraPreview   extends SurfaceView {

    /**
     * 中心点坐标
     */
    private Point centerPoint;

    /**
     * 剪切路径
     */
    private Path clipPath;
    /**
     * 半径
     */
    private int radius;

    public CircleCameraPreview(Context context) {
        super(context);
        init();
    }

    public CircleCameraPreview(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CircleCameraPreview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    /**
     * 初始化
     */
    private void init() {
        clipPath = new Path();
        centerPoint = new Point();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 坐标转换为实际像素
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

            FaceRecognizeActivity.ScreenWidth = widthSize;
        FaceRecognizeActivity.ScreenHeight = heightSize;

        KLog.d("宽"+widthSize+"高"+heightSize);

        // 计算出圆形的中心点
        centerPoint.x = widthSize >> 1;
        centerPoint.y = heightSize >> 1;
        // 计算出最短的边的一半作为半径
        radius = ( centerPoint.x > centerPoint.y) ? centerPoint.y : centerPoint.x;
        KLog.i( "onMeasure: " + centerPoint.toString());
        clipPath.reset();
        clipPath.addCircle(centerPoint.x, centerPoint.y, radius, Path.Direction.CCW);
        setMeasuredDimension(widthSize, heightSize);
    }


    /**
     * 绘制
     *
     * @param canvas 画布
     */
    @Override
    public void draw(Canvas canvas) {
        //裁剪画布，并设置其填充方式
        if (Build.VERSION.SDK_INT >= 26) {
            canvas.clipPath(clipPath);
        } else {
            canvas.clipPath(clipPath, Region.Op.REPLACE);
        }
        super.draw(canvas);
    }

}